
//
//  MonthCardRequestModel.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 16/8/18.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "MonthCardRequestModel.h"

//获取车牌列表
#define MONTH_CARD_LIST [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUser_service,@"/rest-api/v1/parkingAdminServiceController/getCarNumbers.json",nil]
//缴费详情
#define MONTH_CARD_PAY_INFO [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCarPayment_Api,@"/rest-api/v1/parkingPayRestApiController/getCarInfo.json",nil]
//缴费记录
#define MONTH_CARD_PAY_RECORD_LIST [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUser_service,@"/rest-api/v1/parkingAdminServiceController/getPayLogList.json",nil]

//缴费金额
#define MONTH_CARD_PAY_PAYMENT_CHARGE [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCarPayment_Api,@"/rest-api/v1/parkingPayRestApiController/getPaymentCharge.json",nil]
//获取缴费支付列表
#define MONTH_CARD_PAY_METHOD_LIST [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCarPayment_Api,@"/rest-api/v1/parkingPayRestApiController/getPayMethodList.json",nil]

//轮询请求获取支付方式列表
#define kPollByPayMethodList [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rest-api/v1/payment/pollByPayMethodResult.json",nil]


@interface MonthCardRequestModel ()

@property (nonatomic, assign) __block NSInteger requestCount;

@end

@implementation MonthCardRequestModel
/**
 *  单例统一管理MonthCard涉及的网络请求
 */
+ (instancetype)sharedRequestModel
{
    static MonthCardRequestModel* instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    
    return instance;
}

/**
 *  获取车牌列表 可替换[[[BusinessEntityManager sharedManager] NewPublicEntityManager] onRequestMonthCardList:params];
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requestCarNumbersWithCommunityId:(NSString *)communityId finished:(SEGNetworkReponse)finished {
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:communityId, @"communityId", nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST
                                        URLString:MONTH_CARD_LIST
                                       parameters:params
                                         finished:^(BOOL isSuccessed,
                                                    id result,
                                                    NSString *codeType,
                                                    NSString *errorMessage) {
                                             finished(isSuccessed, result, codeType, errorMessage);
                                         }];
}

/**
 *  获取车缴费详情 可替换[[[BusinessEntityManager sharedManager] NewPublicEntityManager] onRequestMonthCardInfo:params];
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requestCarInfoWithDictionary:(NSDictionary *)params finished:(SEGNetworkReponse)finished {
    [SEGHTTPBusinessManager seg_requestWithMethod:POST
                                        URLString:MONTH_CARD_PAY_INFO
                                       parameters:params
                                         finished:^(BOOL isSuccessed,
                                                    id result,
                                                    NSString *codeType,
                                                    NSString *errorMessage) {
                                             finished(isSuccessed, result, codeType, errorMessage);
                                         }];

}

/**
 *  获取缴费记录 可替换[[[BusinessEntityManager sharedManager] NewPublicEntityManager] onRequestPayRecordList:params];
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requestPayRecordList:(SEGNetworkReponse)finished {
    [SEGHTTPBusinessManager seg_requestWithMethod:POST
                                        URLString:MONTH_CARD_PAY_RECORD_LIST
                                       parameters:nil
                                         finished:^(BOOL isSuccessed,
                                                    id result,
                                                    NSString *codeType,
                                                    NSString *errorMessage) {
                                             finished(isSuccessed, result, codeType, errorMessage);
                                         }];
}


/**
 *  获取缴费金额 可替换[[[BusinessEntityManager sharedManager] NewPublicEntityManager] onRequestPaymentCharge:params];
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requestPaymentChargeWithDictionary:(NSDictionary *)params finished:(SEGNetworkReponse)finished {
    [SEGHTTPBusinessManager seg_requestWithMethod:POST
                                        URLString:MONTH_CARD_PAY_PAYMENT_CHARGE
                                       parameters:params
                                         finished:^(BOOL isSuccessed,
                                                    id result,
                                                    NSString *codeType,
                                                    NSString *errorMessage) {
                                             finished(isSuccessed, result, codeType, errorMessage);
                                         }];

}

/**
 *  获取缴费支付列表 可替换[[[BusinessEntityManager sharedManager] NewPublicEntityManager] onRequestPayMethodList:params];
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requestPayMethodListWithCommunityId:(NSString *)communityId finished:(SEGNetworkReponse)finished {
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:communityId, @"communityId", nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST
                                        URLString:MONTH_CARD_PAY_METHOD_LIST
                                       parameters:params
                                         finished:^(BOOL isSuccessed,
                                                    id result,
                                                    NSString *codeType,
                                                    NSString *errorMessage) {
                                             finished(isSuccessed, result, codeType, errorMessage);
                                         }];

}

/**
 *  轮询请求获取支付方式列表 可替换[[BusinessManager sharedManager].electronicBillingEntityManager requestpollByPayMethodList:
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requestPayMethodListWithRequestId:(NSString*)requestId finished:(SEGNetworkReponse)finished
{
    WeakSelf;
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:requestId, @"requestId", nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET
                                        URLString:kPollByPayMethodList
                                       parameters:params
                                         finished:^(BOOL isSuccessed,
                                                    id result,
                                                    NSString *codeType,
                                                    NSString *errorMessage) {
                                             if (isSuccessed) {
                                                 finished(isSuccessed, result, codeType, errorMessage);
                                             }else{
                                                 if (weakSelf.requestCount < 10) { // 请求次数小于10 继续请求
                                                     weakSelf.requestCount ++;
                                                     [NSThread sleepForTimeInterval:1.0f];
                                                     
                                                     [weakSelf requestPayMethodListWithRequestId:requestId finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                                                         finished(isSuccessed, result, codeType, errorMessage);
                                                     }];
                                                 }
                                                 else { // 异常处理
                                                     finished(isSuccessed, result, codeType, errorMessage);
                                                 }
                                             }
                                         }];
}

@end
